package com.example.homeworkdev.pages;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.homeworkdev.R;
import com.example.homeworkdev.adapter.MessageAdapter;
import com.example.homeworkdev.adapter.MessageInfoAdapter;
import com.example.homeworkdev.entity.Message;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MessageInfoActivity extends AppCompatActivity {

    private RecyclerView recyclerViewMessages;
    private EditText editTextMessage;
    private Button buttonSend;
    private MessageInfoAdapter messageInfoAdapter;
    private List<Message> messageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.message_info);

        //获取选取聊天框的基础信息,并初始化界面信息
        Intent intent = getIntent();
        int img = intent.getIntExtra("img",R.drawable.cxk);
        String message = intent.getStringExtra("message");



        // 初始化视图组件
        recyclerViewMessages = findViewById(R.id.recyclerViewMessages);
        editTextMessage = findViewById(R.id.editTextMessage);
        buttonSend = findViewById(R.id.buttonSend);

        // 初始化消息列表
        messageList = new ArrayList<>();
        messageList.add(new Message(img,"name",message,"12:11",false));


        // 设置RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerViewMessages.setLayoutManager(layoutManager);
        messageInfoAdapter = new MessageInfoAdapter(messageList);
        recyclerViewMessages.setAdapter(messageInfoAdapter);


        // 设置发送按钮点击事件
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });

        // 监听RecyclerView的布局变化，确保滚动到底部
        recyclerViewMessages.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                       int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (bottom < oldBottom) {
                    recyclerViewMessages.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerViewMessages.scrollToPosition(messageList.size() - 1);
                        }
                    }, 100);
                }
            }
        });



    }

    private void sendMessage() {
        Intent intent = getIntent();
        int img = intent.getIntExtra("img",R.drawable.cxk);
        String messageText = editTextMessage.getText().toString().trim();
        if (!messageText.isEmpty()) {
            String time = DateFormat.format("HH:mm", new Date()).toString();
            // 自己的头像资源ID（假设为R.drawable.ic_avatar_self）
            Message message = new Message(R.drawable.hajimi,"哈基米",messageText,time,true);
            messageList.add(message);

            // 模拟对方回复
             Message reply = new Message(img,"name","[自动回复]对不起，网路不通，请重新发送！",time,false);
             messageList.add(reply);

            messageInfoAdapter.notifyDataSetChanged();
            recyclerViewMessages.scrollToPosition(messageList.size() - 1);
            editTextMessage.setText("");
        }
    }

    public void fanhui(View view) {
        finish();
    }



}
